package edu.upenn.yas;

import java.util.Date;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/** Action taken when a user logs on */
public class LoginAction extends ActionSupport {
    private String username;
    private String password;

    public String execute() {
        String retrievedPw = QueryEngine.instance.getPassword(username);
        Map session = ActionContext.getContext().getSession();
        if (retrievedPw != null && retrievedPw.equals(password)) {
            session = ActionContext.getContext().getSession();
            session.put("login", "true");
            session.put("username", username);
            session.put("context", new Date());
            session.remove("loginfailed");
            return SUCCESS;
        } else {
            session.put("loginfailed", "true");
            return ERROR;
        }
    }

    public String getUsername() { return username; }
    public void setUsername(String username) { this.username = username; }
    public String getPassword() { return password; }
    public void setPassword(String password) { this.password = password; }
}
